package com.dissertation.service.impl;

import com.dissertation.model.Assets;
import com.dissertation.service.IAssetsFindByTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AssetsFindByTagImpl implements IAssetsFindByTag {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public List<Assets> findByTag( String tagName,
                                   String categoryTreeName,
                                   String categoryLeafName ) {
        Query query = Query.query( where( "categoryTreeName" ).is( categoryTreeName )
                .and( "categoryLeafName" ).is( categoryLeafName )
                .and( "deleted" ).is( false )
                .and( "tags" ).elemMatch( where( "$eq" ).is( tagName ) ) );
        return mongoTemplate.find( query, Assets.class );
    }
}
